package com.jh3502.vivo.function.login;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.EditText;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.function.MainActivity;
import com.jh3502.vivo.utils.SpHelper;
import com.jh3502.vivo.utils.Utils;
import com.jh3502.vivo.view.SnackbarUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cxy.com.validate.SimpleValidateResult;
import cxy.com.validate.Validate;
import cxy.com.validate.annotation.Index;
import cxy.com.validate.annotation.NotNull;

/**
 * author:harisucici on 2018/11/14 10:52
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class LoginActivity extends BaseActivity<LoginPresenter.LoginView, LoginPresenter> implements LoginPresenter.LoginView {

    @Index(0)
    @NotNull(msg = "请输入姓名!")
    @BindView(R.id.login_phone)
    EditText loginPhone;

    @Index(1)
    @NotNull(msg = "请输入密码!")
    @BindView(R.id.login_pass)
    EditText loginPass;

    @Override
    public LoginPresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        String token = SpHelper.getInstance().getToken();
        if (!TextUtils.isEmpty(token)) {
            Utils.getInstance().startActivity(this, MainActivity.class, true);

        }
        loginPhone.setText("user1");
        loginPass.setText("111111");
        mPresenter.setLoginView(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg, SnackbarUtil.RED);
    }

    @Override
    public void loginSuccess() {
        Utils.getInstance().startActivity(this, MainActivity.class, true);
    }


    @OnClick(R.id.login_submit)
    public void onLoginSubmitClicked() {
        if (Utils.getInstance().isFastDoubleClick()) {
            return;
        }
        Validate.check(this, new SimpleValidateResult() {
            @Override
            public void onValidateSuccess() {
                login();
            }

            @Override
            public void onValidateError(@NonNull String msg, EditText editText) {
                showShortToast(msg, SnackbarUtil.ALERT);
            }
        });
    }

    private void login() {
        Map<String, Object> body = new HashMap<>(2);
        body.put("username", loginPhone.getText().toString().trim());
        body.put("password", loginPass.getText().toString().trim());
        mPresenter.toLogin(body);
        mPresenter.toLogin(body);
        mPresenter.toLogin(body);

    }
}
